package com.unionfetch.content_home.ui.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.unionfetch.content_home.R
import com.unionfetch.content_home.adapter.AccountInfoListAdapter
import com.unionftech.common.R.dimen
import com.unionftech.common.R.style
import com.unionftech.common.utils.Utils


class UFAccountInfoDialog(context: Context) :
    Dialog(context) {
    private val mContext = context
    private val mTvTitle: TextView by lazy { findViewById(R.id.tv_title) }
    private val mIvClose: ImageView by lazy { findViewById(R.id.iv_close) }
    private val mRvList: RecyclerView by lazy { findViewById(R.id.rv_list) }
    private val mList = mutableListOf<com.unionfetch.content_home.bean.TradeAccountModel>()
    private lateinit var mAdapter: AccountInfoListAdapter
    private var mTitleText: String = ""
    private var mCallBack: ((Int) -> Unit)? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val contentView =
            LayoutInflater.from(mContext).inflate(R.layout.layout_account_info_dialog, null)
        setCancelable(true)
        setCanceledOnTouchOutside(true)
        setContentView(contentView)
        initView()
        setListener()
    }

    private fun initView() {
        mAdapter = AccountInfoListAdapter(mList)
        mRvList.layoutManager = LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
        mRvList.adapter = mAdapter
    }

    private fun setListener() {
        mIvClose.setOnClickListener {
            dismiss()
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            mCallBack?.invoke(position)
            mRvList.postDelayed({
                dismiss()
            }, 100)
        }
    }

    private fun setView() {
        mTvTitle.text = mTitleText
        mAdapter.notifyDataSetChanged()
    }


    fun setCallBack(callback: (Int) -> Unit): UFAccountInfoDialog {
        this.mCallBack = callback
        return this
    }

    fun setData(list: List<com.unionfetch.content_home.bean.TradeAccountModel>): UFAccountInfoDialog {
        this.mList.clear()
        this.mList.addAll(list)
        return this
    }

    fun setTitle(title: String): UFAccountInfoDialog {
        mTitleText = title
        return this
    }

    override fun onStart() {
        super.onStart()
        window?.decorView?.setPadding(0, 0, 0, 0)
        window?.attributes?.gravity = Gravity.BOTTOM
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window?.attributes?.windowAnimations = style.BottomDialogAnimation
        window?.setLayout(
            WindowManager.LayoutParams.MATCH_PARENT,
            mContext.resources.getDimension(
                dimen.dp_400
            ).toInt()
        )
        window?.also {
            Utils.setDialogNavigationBarTransparent(it)
        }
        setView()
    }

    override fun show() {
        super.show()
    }
}